﻿#include "frwidget.h"
#include "ui_frwidget.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QTextDocument>
#include <QTextBlock>
#include <QTableWidgetItem>

int FRwidget::identification=0;
FRwidget::FRwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FRwidget)
{
    ui->setupUi(this);
    FileReceive();
}

FRwidget::~FRwidget()
{
    delete ui;}

void FRwidget::FileReceive()
{
    ui->FileR->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->FileR->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可修改
    ui->FileR->setColumnHidden(0, true);//隐藏首列id
    ui->FileR->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->FileR->hideColumn(0);//隐藏首列id
    //设置日历控件当前时间
    ui->dtEdtFileStart_2->setDate(QDate::currentDate());
    ui->dtEdtFileEnd->setDate(QDate::currentDate());
    ui->dtEdtLbryStart_2->setDate(QDate::currentDate());
    ui->dtEdtLbryEnd->setDate(QDate::currentDate());
    connect(ui->FileR->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(FRshowsort(int)));
    ui->FileR->setRowCount(0);
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileReceive limit 20;");
    query.exec(strSelect);
    while (query.next()) {
        ui->FileR->insertRow(0);
        for(int a=0 ; a<12 ; a++){
            if(a == 4){//判断证件类型
                int temp = query.value(a).toInt();
                switch (temp) {
                case 1:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("居民身份证")));
                    break;
                case 2:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("军官证")));
                    break;
                case 3:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("士官证")));
                    break;
                case 4:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("义务兵证")));
                    break;
                case 5:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("港澳台居民居住证")));
                    break;
                case 6:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("外籍人士在我国永久居住证")));
                    break;
                case 7:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("护照")));
                    break;
                case 9:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("其他")));
                    break;
                default:
                    break;
                }
            }else{
                ui->FileR->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
            }
        }
    }
}

void FRwidget::on_FRadd_clicked()
{
    AddFileRDlg addFileRDlg(this);
    addFileRDlg.exec();
}

void FRwidget::FRshowdatabase()
{
    ui->FileR->clearContents();
    ui->FileR->setRowCount(0);
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileReceive;");
    query.exec(strSelect);
    while (query.next()) {
        ui->FileR->insertRow(0);
        for(int a=0 ; a<12 ; a++){
            if(a == 4){//判断证件类型
                int temp = query.value(a).toInt();
                switch (temp) {
                case 1:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("居民身份证")));
                    break;
                case 2:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("军官证")));
                    break;
                case 3:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("士官证")));
                    break;
                case 4:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("义务兵证")));
                    break;
                case 5:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("港澳台居民居住证")));
                    break;
                case 6:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("外籍人士在我国永久居住证")));
                    break;
                case 7:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("护照")));
                    break;
                case 9:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("其他")));
                    break;
                default:
                    break;
                }
            }else{
                ui->FileR->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
            }
        }
    }
}

void FRwidget::on_FRdelete_clicked()
{
    QList<QTableWidgetItem*> itemsTemp = ui->FileR->selectedItems();
    int countTemp = itemsTemp.count();
    if(countTemp == 0){//如果未选中记录
        QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning,tr("提示"),tr("请先选中要删除的信息！"), QMessageBox::Abort);
        msgBox->button(QMessageBox::Abort)->setText("确定");
        msgBox->exec();
    }else{
        ui->FileR->showColumn(0);//取id前使隐藏列可见

        QList<QTableWidgetSelectionRange> ranges = ui->FileR->selectedRanges();
        QStringList list;//存放id
        int countRow = ranges.count();
        for(int i=0 ; i < countRow ; i++)
        {
           int topRow=ranges.at(i).topRow();
           int bottomRow=ranges.at(i).bottomRow();
           for(int j=topRow;j<=bottomRow;j++)
           {
              list.append(ui->FileR->item(j,0)->text());
            }
        }
        ui->FileR->setColumnHidden(0, true);//取完id后继续隐藏id
        QSqlQuery query;
        QMessageBox::StandardButton btnDel;
        btnDel = QMessageBox::question(this, "提示", "确定删除吗?", QMessageBox::Yes|QMessageBox::No);
        if (btnDel == QMessageBox::Yes) {
            for(int i = 0; i< list.size();i++)
            {
                QString strItem = list.at(i);
                QString strSql = QString("DELETE FROM FileReceive WHERE `FileRecID`='%1';").arg(strItem);
                bool delFlag = query.exec(strSql);
                if(delFlag == false){
                    QMessageBox::information(this,"提示信息","删除失败!");
                }
            }
        on_FRseek_clicked();
        ui->FileR->selectRow(-1);
        }
    }
}

void FRwidget::on_FRseek_clicked()
{
    int select;
    if(ui->FileR->currentRow()>-1)
    {
        select=ui->FileR->item(ui->FileR->currentRow(),0)->text().toInt();
    }
    QString strFromUnit = ui->edtFromUnit->text().trimmed();
    QString strRecPerson = ui->edtFileToPerson->text().trimmed();
    QString strNowDuty = ui->edtUnitDuty->text().trimmed();
    QString strName = ui->edtFileOfPerson->text().trimmed();
    QString strFileStart = ui->dtEdtFileStart_2->date().toString("yyyy-MM-dd");
    QString strFileEnd = ui->dtEdtFileEnd->date().toString("yyyy-MM-dd");
    QString strLbryStart = ui->dtEdtLbryStart_2->date().toString("yyyy-MM-dd");
    QString strLbryEnd = ui->dtEdtLbryEnd->date().toString("yyyy-MM-dd");
    QString strSql = QString("SELECT * FROM FileReceive WHERE 1=1 ");
    if(!strFromUnit.isEmpty()){
        strSql += QString("AND FileFromUnit LIKE '%%1%'").arg(strFromUnit);
    }
    if(!strRecPerson.isEmpty()){
        strSql += QString("AND FileToUnit LIKE '%%1%'").arg(strRecPerson);
    }
    if(!strNowDuty.isEmpty()){
        strSql += QString("AND FileNowDuty LIKE '%%1%'").arg(strNowDuty);
    }
    if(!strName.isEmpty()){
        strSql += QString("AND FilePerName LIKE '%%1%'").arg(strName);
    }
    if(ui->chkBxStartFile_2->isChecked()){
        strSql += QString("AND FileRecTime >= '%1'").arg(strFileStart);
    }
    if(ui->chkBxEndFile->isChecked()){
        strSql += QString("AND FileRecTime <= '%1'").arg(strFileEnd);
    }
    if(ui->chkBxStartLbry_2->isChecked()){
        strSql += QString("AND FileLbyTime >= '%1'").arg(strLbryStart);
    }
    if(ui->chkBxEndLbry->isChecked()){
        strSql += QString("AND FileLbyTime <= '%1'").arg(strLbryEnd);
    }
    QSqlQuery query;
    query.exec(strSql);
    ui->FileR->clearContents();
    ui->FileR->setRowCount(0);
    while (query.next()) {
        ui->FileR->insertRow(0);
        for(int a=0 ; a<12 ; a++){
            if(a == 4){//判断证件类型
                int temp = query.value(a).toInt();
                switch (temp) {
                case 1:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("居民身份证")));
                    break;
                case 2:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("军官证")));
                    break;
                case 3:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("士官证")));
                    break;
                case 4:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("义务兵证")));
                    break;
                case 5:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("港澳台居民居住证")));
                    break;
                case 6:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("外籍人士在我国永久居住证")));
                    break;
                case 7:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("护照")));
                    break;
                case 9:
                    ui->FileR->setItem(0,a,new QTableWidgetItem(QString("其他")));
                    break;
                default:
                    break;
                }
            }else{
                ui->FileR->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
            }
        }
    }
    if(select>0){
        for(int j=0;j<ui->FileR->rowCount();j++)
            {
           if(ui->FileR->item(j,0)->text().toInt()==select){
               ui->FileR->selectRow(j);
           }
        }
}}

void FRwidget::on_FRedit_clicked()
{
    int i=ui->FileR->currentRow();
    if(i>-1){
        editFileRDialog editfile(this);
    connect(this,SIGNAL(sendData(QStringList)),&editfile,SLOT(receiveInfo(QStringList)));
    QList<QTableWidgetItem*>items=ui->FileR->selectedItems();
    QString str = ui->FileR->item(i,0)->text();
       QString unit=items.at(0)->text();
       QString rcman=items.at(1)->text();
       QString name=items.at(2)->text();
       QString cardtype=items.at(3)->text();
       QString cardID=items.at(4)->text();
       QString sex=items.at(5)->text();
       QString native=items.at(6)->text();
       QString duty=items.at(7)->text();
       QString other=items.at(8)->text();
       QString timer=items.at(9)->text();
       QString timei=items.at(10)->text();
       QStringList fan;
       fan<<str<<unit<<rcman<<name<<cardtype<<cardID<<sex<<native<<duty<<other<<timer<<timei;
    emit sendData(fan);
    editfile.exec();
    on_FRseek_clicked();
    }
    else{
    QMessageBox::information(this,"提示信息","未选中编辑行!");
    }
}

void FRwidget::on_FRPDF_clicked()
{
    int i=ui->FileR->currentRow();
    if(i>-1){
        QString filePth = QCoreApplication::applicationDirPath() + "/";
        QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), filePth, "*.pdf");
        if (!fileName.isEmpty())
        {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
            {
                fileName.append(".pdf");
            }
            QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
            // 指定输出格式为pdf
            printer->setPageSize(QPrinter::A4);
            printer->setOutputFormat(QPrinter::PdfFormat);
            printer->setOutputFileName(fileName);
            QTextDocument text_document;
            text_document.setHtml(FRsaveHtmlToPDF(fileName));
            text_document.print(printer);
            text_document.end();
        }
    }else{
        QMessageBox::information(this,"提示信息","未选择打印条目行!");
    }
}

QString FRwidget::FRsaveHtmlToPDF(QString Q)
{
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title>"+Q+"</title>";
    html += "<head>";
    html += "<body>";
    html += "<font size=\"4\" face=\"arial\">";
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"4\">档案接收表</th>";
    html += "</tr>";
    QList<QTableWidgetSelectionRange> ranges = ui->FileR->selectedRanges();
    int countTemp = ranges.count();
    for(int i=0;i<countTemp;i++)
    {
        int topRow=ranges.at(i).topRow();
        int bottomRow=ranges.at(i).bottomRow();
        for(int j=topRow;j<=bottomRow;j++)
        {
        html += "<tr>";
            html += "<td align=\"center\" >转档单位(人)：</td>";
            html += "<td align=\"center\" >"+ui->FileR->item(j,1)->text()+"</td>";
            html += "<td align=\"center\" >接收人：</td>";
            html += "<td align=\"center\" >"+ui->FileR->item(j,2)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">姓名：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,3)->text()+"</td>";
            html += "<td align=\"center\">性别：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,6)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">证件类型：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,4)->text()+"</td>";
            html += "<td align=\"center\">证件号：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,5)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">籍贯：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,7)->text()+"</td>";
            html += "<td align=\"center\">现单位职务：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,8)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">接收时间：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,10)->text()+"</td>";
            html += "<td align=\"center\">入库时间：</td>";
            html += "<td align=\"center\">"+ui->FileR->item(j,11)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">备注：</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->FileR->item(j,9)->text()+"</td>";
        html += "</tr>";
    }}
    html += "</table>";
    html += "</font>";
    html += "</body>";
    html += "</html>";
    return html;
}

void FRwidget::on_chkBxStartFile_2_clicked(bool checked)
{
    ui->dtEdtFileStart_2->setEnabled(checked);
}

void FRwidget::on_chkBxEndFile_clicked(bool checked)
{
    ui->dtEdtFileEnd->setEnabled(checked);
}

void FRwidget::on_chkBxStartLbry_2_clicked(bool checked)
{
    ui->dtEdtLbryStart_2->setEnabled(checked);
}

void FRwidget::on_chkBxEndLbry_clicked(bool checked)
{
    ui->dtEdtLbryEnd->setEnabled(checked);
}

void FRwidget::FRshowsort(int clounm)
{
    if(identification==1){
    ui->FileR->sortItems(clounm,Qt::DescendingOrder);
    identification=0;}
    else{ui->FileR->sortItems(clounm,Qt::AscendingOrder);
    identification=1;}
}
